package com.ikas.ai.score.autoconfigure;

import com.ikas.ai.framework.FlowEventLoop;
import com.ikas.ai.score.properties.FlowPropertiesConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @Description: 自动创建构建FlowEventLoop
 * @Author: qi.yilai
 * @Date: 2023/1/16 16:55
 * @Version: 1.0.0
 **/
@ConditionalOnProperty(name = "ikas.auto.createFlowEventLoop", havingValue = "true")
@EnableConfigurationProperties(value = {FlowPropertiesConfig.class})
@Slf4j
public class AiMaintainFlowAutoConfigure {

    @Autowired
    private FlowPropertiesConfig flowPropertiesConfig;

    /**
     * 建模使用
     * @return
     */
    @Bean
    public FlowEventLoop createModelingFlowEventLoop() {
        FlowEventLoop flowEventLoop;
        if (flowPropertiesConfig.getCoreSize() <= 0
                || flowPropertiesConfig.getExecutorPoolSize() <= 0
                || flowPropertiesConfig.getReadyThresholdSize() <= 0
                || flowPropertiesConfig.getCheckInterval() <=0
                || flowPropertiesConfig.getExecutorPoolSize() < flowPropertiesConfig.getCoreSize()
                || flowPropertiesConfig.getFlowThreshold() <= 0) {
            flowEventLoop = new FlowEventLoop();
        } else {
            flowEventLoop = new FlowEventLoop(flowPropertiesConfig.getCoreSize(), flowPropertiesConfig.getExecutorPoolSize(), flowPropertiesConfig.getCheckInterval(), flowPropertiesConfig.getReadyThresholdSize(), flowPropertiesConfig.getFlowThreshold());
        }
        log.info("create maintain FlowEventLoop success");
        flowEventLoop.start();
        return flowEventLoop;
    }
}
